package com.longbig.multifunction.service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.longbig.multifunction.convert.SwitchConvert;
import com.longbig.multifunction.dto.*;
import com.longbig.multifunction.helper.ExcelHelper;
import com.longbig.multifunction.mapper.SysGeoAccountInfoMapper;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @date 2024/6/9 10:08
 * @description
 */
@Service
@Slf4j
public class SyncProjectPowerInfoService {

    @Autowired
    private SwitchConvert switchConvert;

    @Autowired
    private SysGeoAccountInfoMapper sysGeoAccountInfoMapper;

    private static List<String> batchDateList = new ArrayList<>();
    static {
        batchDateList.add("2024-04");
        batchDateList.add("2024-05");
    }

    public void pushData() throws Exception {
        File file = new File("D:\\出差绿证项目\\清洗数据\\数据清洗导入\\数据清洗导入\\全周期项目电量信息.xlsx");
        File checkBindFile = new File("D:\\出差绿证项目\\清洗数据\\数据清洗导入\\数据清洗导入\\历史结余电量信息.xlsx");
        InputStream inputStream = new FileInputStream(file);
        InputStream checkBindInputStream = new FileInputStream(checkBindFile);
        List<ProjectPowerInfo> projectPowerInfos = ExcelHelper.readExcel(inputStream, ProjectPowerInfo.class, 0);
        List<CheckBindDTO> checkBindDTOList = ExcelHelper.readExcel(checkBindInputStream, CheckBindDTO.class, 0);
        Map<String, BigDecimal> checkBindDTOMap = checkBindDTOList.stream()
                .collect(Collectors.toMap(CheckBindDTO::getProjectCode, CheckBindDTO::getResidueTotalElec));
        Map<String, Set<String>> prodYearMonthMap = Maps.newHashMap();

        List<ProjectPowerInfoDO> powerInfoDOList = Lists.newArrayList();
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        DateTimeFormatter formatterNew = DateTimeFormatter.ofPattern("yyyy/M/d H:mm:ss");
        log.info("projectPowerInfos size:{}", projectPowerInfos.size());

        for (ProjectPowerInfo projectPowerInfo : projectPowerInfos) {
            if (!batchDateList.contains(projectPowerInfo.getBatchDate())) {
                continue;
            }
            String projectCode = projectPowerInfo.getProjectCode();
            ProjectPowerInfoDO projectPowerInfoDO = switchConvert.powerInfo2DO(projectPowerInfo);
            projectPowerInfoDO.setPushStatus(1);
            projectPowerInfoDO.setApprove(2);
            if (Objects.isNull(projectPowerInfo.getDeclareSelfElec())) {
                projectPowerInfoDO.setDeclareSelfElec(new BigDecimal(0));
            }
            if (Objects.isNull(projectPowerInfo.getDeclareGreenElec())) {
                projectPowerInfoDO.setDeclareGreenElec(new BigDecimal(0));
            }
            projectPowerInfoDO.setDeclareDataType(1);
//            powerInfoDOList.add(projectPowerInfoDO);

            //插入
            Integer year = projectPowerInfoDO.getYear();
            Integer month = projectPowerInfoDO.getMonth();
            String tableName = "public.gec_project_power_info_" + year + "_";
            if (month < 10) {
                tableName = tableName + "0" + month;
            } else {
                tableName = tableName + month;
            }
            powerInfoDOList.add(projectPowerInfoDO);

            List<ProjectPowerInfoDO> projectPowerInfoDOS = new ArrayList<>();
            projectPowerInfoDOS.add(projectPowerInfoDO);
            log.info("插入数据表:{}，数据：{}", tableName, projectPowerInfoDO.getProjectCode());
            sysGeoAccountInfoMapper.insertProjectPowerInfoList(tableName, projectPowerInfoDOS);

            Set<String> yearMonthSet = prodYearMonthMap.get(projectCode);
            if (Objects.nonNull(yearMonthSet)) {
                yearMonthSet.add(year + "-" + month);
            } else {
                yearMonthSet = new HashSet<>();
                yearMonthSet.add(year + "-" + month);
                prodYearMonthMap.put(projectCode, yearMonthSet);
            }
        }

        log.info("过滤后的powerInfoDOList size:{}", powerInfoDOList.size());
        List<String> hasInsertData = Lists.newArrayList();
        Map<String, BigDecimal> statictMap = Maps.newHashMap();
        BigDecimal totalStatic4 = new BigDecimal(0);
        BigDecimal totalStatic5 = new BigDecimal(0);
        BigDecimal green4 = new BigDecimal(0);
        BigDecimal green5 = new BigDecimal(0);

//        for (ProjectPowerInfo projectPowerInfo : projectPowerInfos) {
//            if (!batchDateList.contains(projectPowerInfo.getBatchDate())) {
//                continue;
//            }
//
//
//            //历史电量表处理
//            String projectCode = projectPowerInfo.getProjectCode();
//            String batchDate = projectPowerInfo.getBatchDate();
//            if ("2024-04".equals(batchDate)) {
//                totalStatic4 = totalStatic4.add(projectPowerInfo.getDeclareTotalElec());
//                if (Objects.nonNull(projectPowerInfo.getDeclareGreenElec())) {
//                    green4 = green4.add(projectPowerInfo.getDeclareGreenElec());
//                }
//            } else {
//                totalStatic5 = totalStatic5.add(projectPowerInfo.getDeclareTotalElec());
//                if (Objects.nonNull(projectPowerInfo.getDeclareGreenElec())) {
//                    green5 = green5.add(projectPowerInfo.getDeclareGreenElec());
//                }
//
//            }
//
//            String[] array = batchDate.split("-");
//            Integer year = Integer.valueOf(array[0]);
//            Integer month = Integer.valueOf(array[1]);
//
//            if (hasInsertData.contains(projectPowerInfo.getProjectCode() + year + "-" + month)) {
//                continue;
//            }
//
//            Set<String> yearMonthSet = prodYearMonthMap.get(projectCode);
//            BigDecimal residueTotalElec = checkBindDTOMap.get(projectCode);
//            if (Objects.isNull(yearMonthSet) || yearMonthSet.size() == 0 || Objects.isNull(residueTotalElec)) {
//                continue;
//            }
//
//
//            String tableName = "public.gec_project_first_check_bind_" + year + "_";
//            if (month < 10) {
//                tableName = tableName + "0" + month;
//            } else {
//                tableName = tableName + month;
//            }
//
//            GecProjectFirstCheckBindDO gecProjectFirstCheckBindDO = new GecProjectFirstCheckBindDO();
//            gecProjectFirstCheckBindDO.setProjectCode(projectCode);
//            gecProjectFirstCheckBindDO.setYear(year);
//            gecProjectFirstCheckBindDO.setMonth(month);
//            gecProjectFirstCheckBindDO.setProdYearMonth(JSON.toJSONString(yearMonthSet));
//            gecProjectFirstCheckBindDO.setResidueTotalElec(residueTotalElec);
//            gecProjectFirstCheckBindDO.setWaitIssue(JSON.toJSONString(yearMonthSet));
//            gecProjectFirstCheckBindDO.setIssueStatus(1);
//            gecProjectFirstCheckBindDO.setIssueFirstStatus(1);
//            gecProjectFirstCheckBindDO.setUpdateTime(LocalDateTime.now());
//            gecProjectFirstCheckBindDO.setCreateTime(LocalDateTime.now());
//            log.info("插入数据表:{}，数据：{}", tableName, gecProjectFirstCheckBindDO.getProjectCode());
//            sysGeoAccountInfoMapper.insertOrUpdateFirstCheckBind(tableName, gecProjectFirstCheckBindDO);
//            hasInsertData.add(projectCode + year + "-" + month);
//
//        }

//        //插入static表
//        List<GecProjectGcIssueStatisticDO> list = Lists.newArrayList();
//        GecProjectGcIssueStatisticDO gecProjectGcIssueStatisticDO4 = new GecProjectGcIssueStatisticDO();
//        gecProjectGcIssueStatisticDO4.setIssueBatchYear(2024);
//        gecProjectGcIssueStatisticDO4.setIssueBatchMonth(4);
//        gecProjectGcIssueStatisticDO4.setPushTime(LocalDateTime.now());
//        gecProjectGcIssueStatisticDO4.setPushStatus(1);
//        gecProjectGcIssueStatisticDO4.setTotalElec(totalStatic4);
//        gecProjectGcIssueStatisticDO4.setTotalOnlineElec(new BigDecimal(0));
//        gecProjectGcIssueStatisticDO4.setTotalTsgreenElec(green4);
//        gecProjectGcIssueStatisticDO4.setCreateTime(LocalDateTime.now());
//        gecProjectGcIssueStatisticDO4.setUpdateTime(LocalDateTime.now());
//
//        GecProjectGcIssueStatisticDO gecProjectGcIssueStatisticDO5 = new GecProjectGcIssueStatisticDO();
//        gecProjectGcIssueStatisticDO5.setIssueBatchYear(2024);
//        gecProjectGcIssueStatisticDO5.setIssueBatchMonth(5);
//        gecProjectGcIssueStatisticDO5.setPushTime(LocalDateTime.now());
//        gecProjectGcIssueStatisticDO5.setPushStatus(1);
//        gecProjectGcIssueStatisticDO5.setTotalElec(totalStatic5);
//        gecProjectGcIssueStatisticDO5.setTotalOnlineElec(new BigDecimal(0));
//        gecProjectGcIssueStatisticDO5.setTotalTsgreenElec(green5);
//        gecProjectGcIssueStatisticDO5.setCreateTime(LocalDateTime.now());
//        gecProjectGcIssueStatisticDO5.setUpdateTime(LocalDateTime.now());
//        list.add(gecProjectGcIssueStatisticDO4);
//        list.add(gecProjectGcIssueStatisticDO5);
//        sysGeoAccountInfoMapper.batchInsertGecProjectGcIssueStatistic(list);






//        //插入或者更新
//        int BATCH_SIZE = 500;
//        int size = powerInfoDOList.size();
//        log.info("过滤之后的总数为：{}", size);
//        List<ProjectPowerInfoDO> subList;
//
//        for (int i = 0; i < size; i += BATCH_SIZE) {
//            int end = Math.min(i + BATCH_SIZE, size);
//            subList = powerInfoDOList.subList(i, end);
//            log.info("批量插入或更新数据powerInfo:{}", subList.size());
//            sysGeoAccountInfoMapper.insertProjectPowerInfoList(subList);
//        }
    }

}
